#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <linux/input.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/soundcard.h>
#include <dirent.h>


int ts_x, ts_y;
int flag_x = 0, flag_y = 0;
struct input_event ts;

int lcd_show_bmp(char *pathname, int x0, int y0)
{
	//1、打开设备文件
	int lcd = open("/dev/fb0", O_RDWR);
	if(-1 == lcd)
	{
		printf("lcd open error!\n");
		return -2;
	}
	
	//2、内存映射
	char *ptr =  (char *)mmap(NULL, 800*480*4, PROT_READ|PROT_WRITE, 
	                MAP_SHARED, lcd, 0);
	if(NULL == ptr)
	{
		printf("mmap error!\n");
		return -3;
	}
	//清除背景色
	//bzero(ptr, 800*480*4);
	
	//打开图片
	int bmp = open(pathname, O_RDWR);
	if(-1 == bmp)
	{
		printf("bmp open error!\n");
		return -2;
	}
	
	//读取图片相关信息
	int bmp_size, bmp_width, bmp_height;
	//大小
	lseek(bmp, 2, SEEK_SET);
	read(bmp, &bmp_size, sizeof(bmp_size));
	
	//宽度和高度
	lseek(bmp, 18, SEEK_SET);
	read(bmp, &bmp_width, sizeof(bmp_width));
	read(bmp, &bmp_height, sizeof(bmp_height));
	
	//读取图片的颜色数据
	lseek(bmp, 54, SEEK_SET);
	char *bmp_buf = malloc(bmp_size);	//申请空间
	read(bmp, bmp_buf, bmp_width*bmp_height*3);
 
	
	//对颜色数据进行处理（上下颠倒以及数据混乱）
	int bmp_sum = 0;
	int lcd_sum = 0;
	for(int y=0; y+y0<480 && y<bmp_height; y++)
	{
		for(int x=0; x+x0<800 && x<bmp_width; x++)
		{
			bmp_sum = 3*(x+((bmp_height-1-y)*bmp_width));
			lcd_sum = 4*(800*(y+y0)+x+x0);
			
			//等号的坐标属于lcd屏幕， 等号的右边是bmp图像数据
			ptr[lcd_sum+0] = bmp_buf[bmp_sum+0]; //蓝色数据
			ptr[lcd_sum+1] = bmp_buf[bmp_sum+1]; //绿色数据
			ptr[lcd_sum+2] = bmp_buf[bmp_sum+2]; //红色数据
			ptr[lcd_sum+3] = bmp_buf[bmp_sum+3]; //透明度数据
		}
		//usleep(1);
	}
    //释放相关资源
	munmap(ptr, 800*480*4);
	free(bmp_buf);
	//3、关闭文件
	close(lcd);
	close(bmp);
}

void get_touch()
{
    //1、打开设备文件
    int touch_fd = open("/dev/input/event0", O_RDONLY);
    if(-1 == touch_fd)
    {
        printf("event0 open error!\n");
    }

    while(1)
    {
        read(touch_fd, &ts, sizeof(ts));
	//3、分析数据
        if(EV_ABS == ts.type) //判断是否为触摸屏事件
        {
            if(ABS_X == ts.code)//判断是否为x轴数据
            {
                ts_x = ts.value;
                flag_x = 1;	
            }
            else if(ABS_Y == ts.code) //判断是否为y轴数据
            {
                ts_y = ts.value;
                flag_y = 1;
            }
	}
	if(flag_x == 1 && flag_y == 1){ 
		flag_x = 0;
		flag_y = 0;
		ts_x = ts_x*800/1024;
		ts_y = ts_y*480/600;
		printf("ts_x = %d, ts_y = %d\r\n", ts_x, ts_y); 
		break;                    
        }
    }
    close(touch_fd);
}


void area_switch()
{
	//相册
	if((ts_x > 200 && ts_x < 400) && (ts_y > 400 && ts_y < 460)){
		char temp2[30]; 
		char command2[30];
		int pid = getpid();
		sprintf(temp2, "%d", pid);
		sprintf(command2, "kill -9 %s", temp2);

		int fd = open("/tmp/mypipe", O_RDWR);
		char send_buf[150] = "cd /mnt/udisk/lcd_l && ./huapingApp";
		write(fd, send_buf, sizeof(send_buf));
		printf("send message\r\n");
		printf("send_buf = %s\r\n", send_buf);
		sleep(1);
		system(command2);
	}
	//息屏
	else if((ts_x > 350 && ts_x < 450) && (ts_y > 400 && ts_y < 460)){
		lcd_show_bmp("black.bmp", 0, 0);
		
		char temp2[30]; 
		char command2[30];
		int pid = getpid();
		sprintf(temp2, "%d", pid);
		sprintf(command2, "kill -9 %s", temp2);
		sleep(1);
		system(command2);
	}
	//音乐播放器
	else if((ts_x > 460 && ts_x < 600) && (ts_y > 400 && ts_y < 460)){
		char temp2[30]; 
		char command2[30];
		int pid = getpid();
		sprintf(temp2, "%d", pid);
		sprintf(command2, "kill -9 %s", temp2);

		int fd = open("/tmp/mypipe", O_RDWR);
		char send_buf[150] = "cd /mnt/udisk/music && ./musicAppMplayer";
		write(fd, send_buf, sizeof(send_buf));
		printf("send message\r\n");
		printf("send_buf = %s\r\n", send_buf);
		sleep(1);
		system(command2);
	}

}


int main(int argc, char *argv[])
{
	int ret = mkfifo("/tmp/mypipe", 0777);
	if(ret < 0){
		perror("!!mkfifo!!");
	}
	
	if(fork() == 0){
		//通过管道来接收要执行程序命令，然后执行
		printf("enter child process\r\n");
		char rec_buf[150];
		int fd = open("/tmp/mypipe", O_RDWR);
		read(fd, rec_buf, sizeof(rec_buf));
		printf("receive message\r\n");
		printf("rec_buf = %s\r\n", rec_buf);
		sleep(1);
		system(rec_buf);
	}
	else{
		lcd_show_bmp("maincover.bmp", 0, 0);
		while(1){
			get_touch();
			area_switch();
		}
	}
	
	
}
